package com.henghai.zhenxuan.ui.page.mall;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.henghai.zhenxuan.BR;
import com.henghai.zhenxuan.data.bean.AppListTypeBean;
import com.henghai.zhenxuan.domain.request.AppTypeRequester;
import com.henghai.zhenxuan.ui.page.login.LoginActivity;
import com.henghai.zhenxuan.R;
import com.kongzue.dialogx.dialogs.TipDialog;
import com.kongzue.dialogx.dialogs.WaitDialog;
import com.kunminx.architecture.ui.page.BaseFragment;
import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.page.StateHolder;
import com.kunminx.architecture.ui.state.State;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

public class MallFragment extends BaseFragment {
    private MallStates mStates;
    private AppTypeRequester mAppTypeRequester;
    private RefreshLayout mRefreshLayout;

    @Override
    protected void initViewModel() {
        mStates = getFragmentScopeViewModel(MallStates.class);
        mAppTypeRequester = getFragmentScopeViewModel(AppTypeRequester.class);
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mAppTypeRequester.getAppTypeResult().observe(this, httpBeanDataResult -> {
            if (mRefreshLayout != null) {
                mRefreshLayout.finishRefresh();
            }
            if (!httpBeanDataResult.getResponseStatus().isSuccess()) {
                if (httpBeanDataResult.getResponseStatus().getResponseCode().equals(getString(R.string.http_token_error))) {
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                    getActivity().finish();
                }
                TipDialog.show(httpBeanDataResult.getResponseStatus().getResponseCode(), WaitDialog.TYPE.ERROR);
                return;
            }
            mStates.tabList.set(httpBeanDataResult.getResult().getData().getRows());
        });
        mAppTypeRequester.requestAppListTask(this, "1", "50", "product_type");
    }


    public class ListenerRefreshHandler implements OnRefreshListener {
        @Override
        public void onRefresh(@NonNull RefreshLayout refreshLayout) {
            mRefreshLayout = refreshLayout;
            mAppTypeRequester.requestAppListTask(getViewLifecycleOwner(), "1", "50", "product_type");
        }
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(com.henghai.zhenxuan.R.layout.fragment_mall, BR.vm, mStates)
                .addBindingParam(BR.listenerRefresh, new ListenerRefreshHandler())
                .addBindingParam(BR.click, new ClickProxy());
    }

    public class ClickProxy {
        public void search() {
            nav().navigate(R.id.action_mainFragment_to_SearchFragment);
        }
    }


    public static class MallStates extends StateHolder {
        public final State<ArrayList<AppListTypeBean.RowsBean>> tabList = new State<>(new ArrayList<>());

    }

}
